package com.bj58.mis.apm.server.kylinreceive.controller;

import com.bj58.mis.apm.server.core.common.result.ResultBean;
import com.bj58.mis.apm.server.core.util.JsonUtil;
import com.bj58.mis.apm.server.kylinreceive.service.SearchService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/kylin")
public class TimeController {

    @Resource
    private SearchService searchService;

    @ResponseBody
    @RequestMapping("/lasttime")
    public String receive(){
        Long last = TimeForSearch.getLastTime();
        if(last == null) {
            Date lasttime = searchService.queryLastModified();
            if (lasttime != null) {
                last = lasttime.getTime();
            }
        }
        Map<String, Long> result = new HashMap<>();
        result.put("lasttime",last);
        return JsonUtil.toJson(result);
    }

    @RequestMapping("/updateSetting")
    public void updateAllByNextTime(@RequestParam(name="execTime", required = false) Long execTime,
                                    @RequestParam(name="beginTime", required = false) Long beginTime,
                                    @RequestParam(name="allDataUpdate", required = false) Boolean allDataUpdate){
        TimeForSearch.setAllDataUpdateTrue(execTime, beginTime, allDataUpdate);
    }
    @RequestMapping("/getSetting")
    public ResultBean<Map> getSettingForUpdate(){
        Map<String, Object> result = TimeForSearch.getValues();
        return ResultBean.success(result);
    }

}
class TimeForSearch {
    private static boolean allDataUpdate = false;
    private static Long execTime = 0L;
    private static Long beginTime = 0L;
    public static void setAllDataUpdateTrue(Long execTime, Long beginTime, Boolean allDataUpdate){
        if (execTime != null) {
            TimeForSearch.execTime = execTime;
        }else{
            TimeForSearch.execTime = 0L;
        }
        if(beginTime != null && beginTime > 0L){
            TimeForSearch.beginTime = beginTime;
        }else{
            TimeForSearch.beginTime = 0L;
        }
        if(allDataUpdate != null && allDataUpdate) {
            TimeForSearch.allDataUpdate = allDataUpdate;
        }else{
            TimeForSearch.allDataUpdate = false;
        }
    }
    public static Long getLastTime(){
        if(allDataUpdate && new Date().getTime() > execTime){
            allDataUpdate = false;
            return beginTime;
        }
        return null;
    }
    public static Map<String, Object> getValues(){
        Map<String, Object> map = new HashMap<>();
        map.put("allDataUpdate", allDataUpdate);
        map.put("execTime", execTime);
        map.put("beginTime", beginTime);
        return map;
    }
}